<?php

class DataflowAction extends Action {
    public function index(){
    	
    	if ($_POST['startdate']&&$_POST['enddate']){//时间间查询
    			$this->date_sum($_POST['startdate'], $_POST['enddate']);	
    	}elseif($_POST['in_pv_ip_uv']){//排序
    		dump($_SESSION['volist']);
    		foreach ($_SESSION['volist'] as $key => $row) {
			    $volume[$key]  = $row[$_POST['in_pv_ip_uv']];  
			}
			array_multisort($volume, SORT_DESC, $_SESSION['volist']);
    	}elseif($_GET['link_date']){//最近几天数据
    		if($_GET['link_date']==1){//昨天的数据
    			$tmp=time() - 86400*1;
    		}
    		if($_GET['link_date']==7){//昨天的数据
    			$tmp=time() - 86400*7;
    		}
    		if($_GET['link_date']==30){//昨天的数据
    			$tmp=time() - 86400*30;
    		}
    			$enddate=date("Y-m-d",time());
				$startdate=date("Y-m-d",$tmp);				
				$this->date_sum($startdate, $enddate);
    	}else {
				$app_sort=3;
				$info_model=M('App_info');
    			if ($_POST['category_select']=="1"){
					$app_sort=1;
					$map="app_sort=".$app_sort;
				}
    			if ($_POST['category_select']=="2"){
					$app_sort=2;
					$map="app_sort=".$app_sort;
				}
		    	if ($_POST['guanjianzi']){
		    		$map['app_name'] = array('like','%'.$_POST['guanjianzi'].'%');	
		    	}

		    	$this->_list($info_model,$map);
		    	

		    	$time=time();
		    	$app_date_tm=date("Y-m-d",$time);
		    	foreach ($_SESSION['volist'] as $key => $value){
		    		$app_table_ip="app_table_ip_".$value['id'];
					$app_table_ip_model = M($app_table_ip); 
					$app_table_ip_info=$app_table_ip_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_ip_info){
						$app_table_ip_info[0]['aggregate']=0;
					}
		    		$_SESSION['volist'][$key]['ip']=$app_table_ip_info[0]['aggregate'];
		    		
		    		$app_table_pv="app_table_pv_".$value['id'];
					$app_table_pv_model = M($app_table_pv); 
					$app_table_pv_info=$app_table_pv_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_pv_info){
						$app_table_pv_info[0]['aggregate']=0;
					}
		    		$_SESSION['volist'][$key]['pv']=$app_table_pv_info[0]['aggregate'];
		    		
		    		$app_table_uv="app_table_uv_".$value['id'];
					$app_table_uv_model = M($app_table_uv); 
					$app_table_uv_info=$app_table_uv_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_uv_info){
						$app_table_uv_info[0]['aggregate']=0;
					}
		    		$_SESSION['volist'][$key]['uv']=$app_table_uv_info[0]['aggregate'];
		    	}	
    	}
//dump($_SESSION['volist']);
    	$this->assign('list',$_SESSION['volist']);// 赋值数据集
    	$this->assign('app_sort',$app_sort);
    	$this->display();
    }
    
    public function view_index(){
    	if($_GET['app_id']){
		    	$app_id=$_GET['app_id'];
		   		 $time=time();
		    	$app_date_tm=date("Y-m-d",$time);
		    	
		  	  	$app_table_pv="app_table_pv_".$app_id;
				$app_table_pv_model = M($app_table_pv); 
				$app_table_pv_info=$app_table_pv_model->where("app_date='".$app_date_tm."'")->select();
		    	foreach ($app_table_pv_info as $key => $row_pv) {   		
			    		$row_pv_val=$row_pv;
				}
		    	
		    	$app_table_ip="app_table_ip_".$app_id;
				$app_table_ip_model = M($app_table_ip); 
				$app_table_ip_info=$app_table_ip_model->where("app_date='".$app_date_tm."'")->select();		
		    	foreach ($app_table_ip_info as $key => $row_ip) {  		
			    		$row_ip_val=$row_ip;
				}
				
		   	 	$app_table_uv="app_table_uv_".$app_id;
				$app_table_uv_model = M($app_table_uv); 
				$app_table_uv_info=$app_table_uv_model->where("app_date='".$app_date_tm."'")->select();		
		    	foreach ($app_table_uv_info as $key => $row_uv) {  		
			    		$row_uv_val=$row_uv;
				}
					
					$_SESSION['day_list']=array('01'=>array('pv'=>$row_pv_val['01'],'ip'=>$row_ip_val['01'],'uv'=>$row_uv_val['01']),
			    								'02'=>array('pv'=>$row_pv_val['02'],'ip'=>$row_ip_val['02'],'uv'=>$row_uv_val['02']),
			    								'03'=>array('pv'=>$row_pv_val['03'],'ip'=>$row_ip_val['03'],'uv'=>$row_uv_val['03']),
			    								'04'=>array('pv'=>$row_pv_val['04'],'ip'=>$row_ip_val['04'],'uv'=>$row_uv_val['04']),
										    	'05'=>array('pv'=>$row_pv_val['05'],'ip'=>$row_ip_val['05'],'uv'=>$row_uv_val['05']),
										    	'06'=>array('pv'=>$row_pv_val['06'],'ip'=>$row_ip_val['06'],'uv'=>$row_uv_val['06']),
										    	'07'=>array('pv'=>$row_pv_val['07'],'ip'=>$row_ip_val['07'],'uv'=>$row_uv_val['07']),
										    	'08'=>array('pv'=>$row_pv_val['08'],'ip'=>$row_ip_val['08'],'uv'=>$row_uv_val['08']),
										    	'09'=>array('pv'=>$row_pv_val['09'],'ip'=>$row_ip_val['09'],'uv'=>$row_uv_val['09']),
										    	'10'=>array('pv'=>$row_pv_val['10'],'ip'=>$row_ip_val['10'],'uv'=>$row_uv_val['10']),
										    	'11'=>array('pv'=>$row_pv_val['11'],'ip'=>$row_ip_val['11'],'uv'=>$row_uv_val['11']),
										    	'12'=>array('pv'=>$row_pv_val['12'],'ip'=>$row_ip_val['12'],'uv'=>$row_uv_val['12']),
										    	'13'=>array('pv'=>$row_pv_val['13'],'ip'=>$row_ip_val['13'],'uv'=>$row_uv_val['13']),
										    	'14'=>array('pv'=>$row_pv_val['14'],'ip'=>$row_ip_val['14'],'uv'=>$row_uv_val['14']),
										    	'15'=>array('pv'=>$row_pv_val['15'],'ip'=>$row_ip_val['15'],'uv'=>$row_uv_val['15']),
										    	'16'=>array('pv'=>$row_pv_val['16'],'ip'=>$row_ip_val['16'],'uv'=>$row_uv_val['16']),
										    	'17'=>array('pv'=>$row_pv_val['17'],'ip'=>$row_ip_val['17'],'uv'=>$row_uv_val['17']),
										    	'18'=>array('pv'=>$row_pv_val['18'],'ip'=>$row_ip_val['18'],'uv'=>$row_uv_val['18']),
										    	'19'=>array('pv'=>$row_pv_val['19'],'ip'=>$row_ip_val['19'],'uv'=>$row_uv_val['19']),
										    	'20'=>array('pv'=>$row_pv_val['20'],'ip'=>$row_ip_val['20'],'uv'=>$row_uv_val['20']),
										    	'21'=>array('pv'=>$row_pv_val['21'],'ip'=>$row_ip_val['21'],'uv'=>$row_uv_val['21']),
										    	'22'=>array('pv'=>$row_pv_val['22'],'ip'=>$row_ip_val['22'],'uv'=>$row_uv_val['22']),
										    	'23'=>array('pv'=>$row_pv_val['23'],'ip'=>$row_ip_val['23'],'uv'=>$row_uv_val['23']),
												'24'=>array('pv'=>$row_pv_val['24'],'ip'=>$row_ip_val['24'],'uv'=>$row_uv_val['24']),
			    								);
			   //链接
			   $app_link_model=M('app_link');
			   $app_link_info=$app_link_model->where("app_id='".$app_id."'")->select();
			   $j=0;
			   for($num_i=1;$num_i<=5;$num_i++){
			   	   if ($app_link_info[0]['app_link_ad'.$num_i]){
			   	   			$ad_start=$app_link_info[0]['app_link_start'.$num_i];
			   	   			
/*			   	   			if ($app_link_info[0]['app_link_start'.$num_i]==0){
			   	   				$ad_sum=0;
			   	   			}else {
			   	   				$ad_sum=25;
			   	   			}*/
			   	   			
			   	   			if ($ad_start){
			   	   				for ($ad_start;$ad_start<=24;$ad_start++){
	//						   		dump($app_link_info[0]['app_link_start1']);
									$test[]='ttt';
							   		$link_info_num[$j][]=$_SESSION['day_list'][$ad_start];
							   		
							   	}
			   	   			}
//			   	   			dump($j);
//							   	echo "<br/>====";dump($link_info_num[$j]); echo "<br/>====";
					  		for ($i=1;$i<=$app_link_info[0]['app_link_sed'.$num_i];$i++){
					  			if ($i<10){
					  				$i="0".$i;
					  			}
//					  			$link_info_num[$j][]=$_SESSION['day_list'][$i];
//						   		dump($i);
						   	}
						   	
//						   	dump($link_info_num[$j]);
						   foreach ($link_info_num[$j] as  $k=>$row) {
						   		$link_pv[]=$row['pv'];
						   		$link_ip[]=$row['ip'];
						   		$link_uv[]=$row['uv'];
						   		  
							}
							dump($link_pv);
							$link_info_num_list[$app_link_info[0]['app_link_alias'.$num_i]]['link']=$app_link_info[0]['app_link_ad'.$num_i];
							$link_info_num_list[$app_link_info[0]['app_link_alias'.$num_i]]['date']=$app_link_info[0]['app_link_start'.$num_i]."-".$app_link_info[0]['app_link_sed'.$num_i];
							$link_info_num_list[$app_link_info[0]['app_link_alias'.$num_i]]['pv']=array_sum($link_pv);
							$link_info_num_list[$app_link_info[0]['app_link_alias'.$num_i]]['ip']=array_sum($link_ip);
							$link_info_num_list[$app_link_info[0]['app_link_alias'.$num_i]]['uv']=array_sum($link_uv);
					   }
					   $j++;
			   }
					
		   dump($link_info_num_list);
    	    }
    	    if($_POST['in_pv_ip_uv']){
    	    	foreach ($_SESSION['day_list'] as $key => $row) {
			    $volume[$key]  = $row[$_POST['in_pv_ip_uv']];  
				}
				array_multisort($volume, SORT_DESC, $_SESSION['day_list']);
    	    }
    	    		    	
//		dump($_SESSION['day_list']);
		$this->assign('list',$_SESSION['day_list']);// 赋值数据集
		$this->display();
    }
    
    public function add_link(){
    	$tianjia_html='';
    	$this->display();
    }
    
    public function add_link_operation(){
	    if ($_POST['app_name']){
				$info_add_model=M('App_info');
				$vo=$info_add_model->create();
				if ($vo) {
		            if (false !== $info_add_model->add()) {
		            	$app_id=$info_add_model->getLastInsID();
		            	//创建ip表
		            	$ip_Model = new Model();
		            	$table="app_table_ip_".$app_id;
		            	$sql_ip="CREATE TABLE $table (
		            								`app_table_ip_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
		            								`app_id` INT(11) NOT NULL ,
		            								`app_date` VARCHAR(25) NOT NULL ,
		            								`01` INT(11) NOT NULL ,
		            								`02` INT(11) NOT NULL ,
		            								`03` INT(11) NOT NULL ,
		            								`04` INT(11) NOT NULL ,
		            								`05` INT(11) NOT NULL ,
		            								`06` INT(11) NOT NULL ,
		            								`07` INT(11) NOT NULL ,
		            								`08` INT(11) NOT NULL ,
		            								`09` INT(11) NOT NULL ,
		            								`10` INT(11) NOT NULL ,
		            								`11` INT(11) NOT NULL ,
		            								`12` INT(11) NOT NULL ,
		            								`13` INT(11) NOT NULL ,
		            								`14` INT(11) NOT NULL ,
		            								`15` INT(11) NOT NULL ,
		            								`16` INT(11) NOT NULL ,
		            								`17` INT(11) NOT NULL ,
		            								`18` INT(11) NOT NULL ,
		            								`19` INT(11) NOT NULL ,
		            								`20` INT(11) NOT NULL ,
		            								`21` INT(11) NOT NULL ,
		            								`22` INT(11) NOT NULL ,
		            								`23` INT(11) NOT NULL ,
		            								`24` INT(11) NOT NULL ,
		            								`aggregate` INT(11) NOT NULL )";
		            	$ip_Model->query($sql_ip);
		            	//创建pv表
		            	$pv_Model = new Model();
		            	$table="app_table_pv_".$app_id;
		            	$sql_pv="CREATE TABLE $table (
		            								`app_table_pv_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
		            								`app_id` INT(11) NOT NULL ,
		            								`app_date` VARCHAR(25) NOT NULL ,
		            								`01` INT(11) NOT NULL ,
		            								`02` INT(11) NOT NULL ,
		            								`03` INT(11) NOT NULL ,
		            								`04` INT(11) NOT NULL ,
		            								`05` INT(11) NOT NULL ,
		            								`06` INT(11) NOT NULL ,
		            								`07` INT(11) NOT NULL ,
		            								`08` INT(11) NOT NULL ,
		            								`09` INT(11) NOT NULL ,
		            								`10` INT(11) NOT NULL ,
		            								`11` INT(11) NOT NULL ,
		            								`12` INT(11) NOT NULL ,
		            								`13` INT(11) NOT NULL ,
		            								`14` INT(11) NOT NULL ,
		            								`15` INT(11) NOT NULL ,
		            								`16` INT(11) NOT NULL ,
		            								`17` INT(11) NOT NULL ,
		            								`18` INT(11) NOT NULL ,
		            								`19` INT(11) NOT NULL ,
		            								`20` INT(11) NOT NULL ,
		            								`21` INT(11) NOT NULL ,
		            								`22` INT(11) NOT NULL ,
		            								`23` INT(11) NOT NULL ,
		            								`24` INT(11) NOT NULL ,
		            								`aggregate` INT(11) NOT NULL )";
		            	$pv_Model->query($sql_pv);
		            	//创建uv表
		            	$uv_Model = new Model();
		            	$table="app_table_uv_".$app_id;
		            	$sql_uv="CREATE TABLE $table (
		            								`app_table_uv_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
		            								`app_id` INT(11) NOT NULL ,
		            								`app_date` VARCHAR(25) NOT NULL ,
		            								`01` INT(11) NOT NULL ,
		            								`02` INT(11) NOT NULL ,
		            								`03` INT(11) NOT NULL ,
		            								`04` INT(11) NOT NULL ,
		            								`05` INT(11) NOT NULL ,
		            								`06` INT(11) NOT NULL ,
		            								`07` INT(11) NOT NULL ,
		            								`08` INT(11) NOT NULL ,
		            								`09` INT(11) NOT NULL ,
		            								`10` INT(11) NOT NULL ,
		            								`11` INT(11) NOT NULL ,
		            								`12` INT(11) NOT NULL ,
		            								`13` INT(11) NOT NULL ,
		            								`14` INT(11) NOT NULL ,
		            								`15` INT(11) NOT NULL ,
		            								`16` INT(11) NOT NULL ,
		            								`17` INT(11) NOT NULL ,
		            								`18` INT(11) NOT NULL ,
		            								`19` INT(11) NOT NULL ,
		            								`20` INT(11) NOT NULL ,
		            								`21` INT(11) NOT NULL ,
		            								`22` INT(11) NOT NULL ,
		            								`23` INT(11) NOT NULL ,
		            								`24` INT(11) NOT NULL ,
		            								`aggregate` INT(11) NOT NULL )";
		            	$uv_Model->query($sql_uv);
		            	//添加连接
				            if ($app_id){
				            	$_POST['app_id']=$app_id;
								$link_add_model=M('App_link');
								$vo=$link_add_model->create();
								if ($vo) {
						            if (false !== $link_add_model->add()) {
						                $this->redirect('Dataflow/index', array(),1,'<script language="JavaScript">alert("添加成功")</script>');
						            } else {
						                $this->error("添加失败");
						            }
						        } else {
						            header("Content-Type:text/html; charset=utf-8");
						            $this->error($link_add_model->getError().' [ <a href="javascript:history.back()">return</a> ]');
						            exit();
						        }
							}
		                $this->redirect('Dataflow/index', array(),1,'<script language="JavaScript">alert("添加成功")</script>');
		            } else {
		                $this->error("添加失败");
		            }
		        } else {
		            header("Content-Type:text/html; charset=utf-8");
		            $this->error($info_add_model->getError().' [ <a href="javascript:history.back()">return</a> ]');
		            exit();
		        }
			}
    	
    }
    
	protected function _list($modelname, $map = '', $field = '*',$asc = false ,$listRows = 100) {
		
		$count = $modelname->where($map)->count();
		if($count>0){
			import ( "@.ORG.Page" );
			$sort = $asc ? 'asc' : 'desc';
			$p = new Page ($count, $listRows );
			$page =$p->show();

			$_SESSION['volist']=$voList = $modelname->field($field)->where($map)->order($asc)->limit($p->firstRow . ','.$p->listRows)->select();			
//			dump($voList);
			
 			$this->assign('page',$page);// 赋值分页输出
		}
	}
	
	public function edit_index(){
		
		$app_id=$_GET['app_id'];
		$app_info_guanlian_model=D('Admin/App_info');
		$app_info_guanlian = $app_info_guanlian_model->relation(true)->where("id='".$app_id."'")->select();
		
		$js_i=0;
		if(!empty($app_info_guanlian[0]['app_link'][0]['app_link_ad2'])){$js_i=1;}
		if(!empty($app_info_guanlian[0]['app_link'][0]['app_link_ad3'])){$js_i=2;}
		if(!empty($app_info_guanlian[0]['app_link'][0]['app_link_ad4'])){$js_i=3;}
		if(!empty($app_info_guanlian[0]['app_link'][0]['app_link_ad5'])){$js_i=4;}
		
		
		$this->assign('js_i',$js_i);
		$this->assign('app_info',$app_info_guanlian);
		$this->display();
	}
	
	public function edit_link_operation(){
		$app_info_guanlian_model=D('Admin/App_info');
		$app_id=$_POST['app_id'];
		$app_link_id=$_POST['app_link_id'];
		
		$data['app_name']=$_POST['app_name'];
        $data['app_sort']=$_POST['app_sort'];
        $data['app_link']=array(
                          array('app_link_id'=>$app_link_id,'app_link_alias1'=>$_POST['app_link_alias1'],'app_link_ad1'=>$_POST['app_link_ad1'],'app_link_start1'=>$_POST['app_link_start1'],'app_link_sed1'=>$_POST['app_link_sed1'],
                          		'app_link_alias2'=>$_POST['app_link_alias2'],'app_link_ad2'=>$_POST['app_link_ad2'],'app_link_start2'=>$_POST['app_link_start2'],'app_link_sed2'=>$_POST['app_link_sed2'],
                          		'app_link_alias3'=>$_POST['app_link_alias3'],'app_link_ad3'=>$_POST['app_link_ad3'],'app_link_start3'=>$_POST['app_link_start3'],'app_link_sed3'=>$_POST['app_link_sed3'],
                          		'app_link_alias4'=>$_POST['app_link_alias4'],'app_link_ad4'=>$_POST['app_link_ad4'],'app_link_start4'=>$_POST['app_link_start4'],'app_link_sed4'=>$_POST['app_link_sed4'],
                          		'app_link_alias5'=>$_POST['app_link_alias5'],'app_link_ad5'=>$_POST['app_link_ad5'],'app_link_start5'=>$_POST['app_link_start5'],'app_link_sed5'=>$_POST['app_link_sed5'],
                          		'app_link_qiyong1'=>$_POST['app_link_qiyong1'],'app_link_qiyong2'=>$_POST['app_link_qiyong2'],'app_link_qiyong3'=>$_POST['app_link_qiyong3'],'app_link_qiyong4'=>$_POST['app_link_qiyong4'],'app_link_qiyong5'=>$_POST['app_link_qiyong5']
                          ),
//                          array('id'=>2,'title'=>'燕子'),
                      );
        //关联更新
       $list=$app_info_guanlian_model->relation(true)->where("id='".$app_id."'")->save($data);
       
       
		 if (false !== $list) {
				$this->redirect('Dataflow/index', array(),1,'<script language="JavaScript">alert("修改成功")</script>');
		} else {
                $this->error("修改失败");
		}
		
	}
	
	private function date_sum($startdate,$enddate){
				$info_model=M('App_info');
    			$this->_list($info_model);
    			$data['app_date'] = array('between', array($startdate, $enddate));
    			foreach ($_SESSION['volist'] as $key => $value){
		    		$app_table_ip="app_table_ip_".$value['id'];
					$app_table_ip_model = M($app_table_ip); 
					$app_table_ip_info=$app_table_ip_model->where($data)->sum('aggregate');
					if (!$app_table_ip_info){
						$app_table_ip_info=0;
					}
					
		    		$_SESSION['volist'][$key]['ip']=$app_table_ip_info;
		    		
		    		$app_table_pv="app_table_pv_".$value['id'];
					$app_table_pv_model = M($app_table_pv); 
					$app_table_pv_info=$app_table_pv_model->where($data)->sum('aggregate');
					if (!$app_table_pv_info){
						$app_table_pv_info=0;
					}
					
		    		$_SESSION['volist'][$key]['pv']=$app_table_pv_info;
		    		
		    		$app_table_uv="app_table_uv_".$value['id'];
					$app_table_uv_model = M($app_table_uv); 
					$app_table_uv_info=$app_table_uv_model->where($data)->sum('aggregate');
					if (!$app_table_uv_info){
						$app_table_uv_info=0;
					}
					
		    		$_SESSION['volist'][$key]['uv']=$app_table_uv_info;
    			}
	}
}